<?php
	require_once (dirname(__FILE__)."/../config/api.inc");
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once(ROOTDIR.'/source/service/class/class.weixin.php');
	header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
	header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
	define('APPID', 'wxe93a5b09e46e21b3');
	define('APPSECRET', 'e3f5b58fc8f820113c4aab8a0bf87753');
	
	$parm = requestArrayParm($_REQUEST);
	
	$url = $parm['url'];
	
	$timestamp = time();
	$wxnonceStr = 'bizhuan';
	$wxticket = getTicket();
	
	$wxOri = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s",$wxticket,$wxnonceStr,$timestamp,$url);
	$wxSha1 = sha1($wxOri);
	
	unset($res);
	
	$res['appId'] = APPID;
	$res['timestamp'] = $timestamp;
	$res['nonceStr'] = $wxnonceStr;
	$res['signature'] = $wxSha1;
	$res['ticket']=$wxticket;

	
	echoJSON($res);
	
	function getTicket()
	{
		//次数修改为通过mongodb记录
		unset($query);
		
		$query['appid'] = APPID;
		$query['style'] = 'ticket';
		$res = _WeiXin::getWeiXin_Info($query);
		
		if(!empty($res))
		{
			if($res['addtime'] >= time())
			{
				$ticket = $res['access_token'];
			}
		}
		
		if(empty($ticket))
		{
			$token = getAccessToken();
			if(empty($token)){
				echo 'no access_token';
				exit;
			}
		
			$url = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi",$token);

			$res = curlGetHttps($url);
			$res = json_decode($res,true);
			$ticket = $res['ticket'];

			if(!empty($ticket))
			{
				unset($query);
		
				$query['appid'] = APPID;
				$query['style'] = 'ticket';
				$info = _WeiXin::getWeiXin_Info($query);	
				
				if(!empty($info))
				{
					unset($query,$data);
					$query['appid'] = APPID;
					$query['style'] = 'ticket';
					$data['access_token'] = c;
					$data['addtime'] = time() + 5400;
					_WeiXin::setWeiXin_Info($query, $data);
				}
				else
				{
					unset($query,$data);
					$data['appid'] = APPID;
					$data['style'] = 'ticket';
					$ticket = $data['access_token'] = $ticket;
					$data['addtime'] = time() + 5400;
					
					_WeiXin::addWeiXin_Info($data);
				}
				
				
				
			}
		}
		

		return $ticket;
	}

	function getAccessToken()
	{
		//次数修改为通过mongodb记录
		unset($query);
		
		$query['appid'] = APPID;
		$query['style'] = 'token';
		$res = _WeiXin::getWeiXin_Info($query);
		
		if(!empty($res))
		{
			if($res['time'] >= time())
			{
				$access_token = $res['access_token'];
			}
		}
		
		if(empty($access_token))
		{
			$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.APPID.'&secret='.APPSECRET;
			$res_json = curlGetHttps($url);
			$res = json_decode($res_json,true);
			
			if(!empty($res['access_token']))
			{
				unset($query);
		
				$query['appid'] = APPID;
				$query['style'] = 'token';
				$info = _WeiXin::getWeiXin_Info($query);			
				
				if(!empty($info))
				{
					unset($query,$data);
					$query['appid'] = APPID;
					$query['style'] = 'token';
					$access_token = $data['access_token'] = $res['access_token'];
					$data['addtime'] = time() + 5400;
					
					_WeiXin::setWeiXin_Info($query, $data);
				}		
				else
				{
					unset($query,$data);
					$data['appid'] = APPID;
					$data['style'] = 'token';
					$access_token = $data['access_token'] = $res['access_token'];
					$data['addtime'] = time() + 5400;
					
					_WeiXin::addWeiXin_Info($data);
				}	
			}
		}

		return $access_token;
	}
	
	function curlGetHttps($url,$sec=5){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_TIMEOUT, $sec);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
